class StylesController < ApplicationController
  # GET /styles
  # GET /styles.xml
  def index
    @styles = Style.paginate :conditions=>{:active=>1},:page => params[:page],:per_page =>28,:order=>"id desc"

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => Style.get_all_for_xml.to_xml(:skip_types => true,:dasherize => false,
          :except => [:active,:created_at,:updated_at,:sort_order,:image_url,:back_image_url,:sn,
          :description,:tee_size],
          :include=>{:materials=>{:except => [:description,:detail_image_url1,:image_url,:active,:quantity,
                :detail_image_url2,:style_id,:updated_at,:sort_order,:created_at,:back_image_url,:tee_size],
              :methods =>[:image_path,:thumb_image_path,:back_image_path]}},
          :methods =>[:cate])}
    end
  end

  # GET /styles/1
  # GET /styles/1.xml
#  def show
#    @style = Style.find(params[:id])
#
#    #    respond_to do |format|
#    #      format.html # show.html.erb
#    #      format.xml  { render :xml => @style.to_xml(:skip_types => true,:dasherize => false,
#    #          :except => [:active,:created_at,:updated_at,:sort_order,:image_url],
#    #          :include=>:materials,
#    #          :methods =>[:image_path,:thumb_image_path])}
#    #    end
#    render :text=>@style.image_url
#  end

  # GET /styles/new
  # GET /styles/new.xml
  def new
    @style = Style.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @style }
    end
  end

  # GET /styles/1/edit
  def edit
    @style = Style.find(params[:id])
  end

  # POST /styles
  # POST /styles.xml
  def create
    @style = Style.new(params[:style])

    respond_to do |format|
      if @style.save
        flash[:notice] = '款式新增成功.'
        format.html { redirect_to(styles_url) }
        format.xml  { render :xml => @style, :status => :created, :location => @style }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @style.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /styles/1
  # PUT /styles/1.xml
  def update
    @style = Style.find(params[:id])

    respond_to do |format|
      if @style.update_attributes(params[:style])
        flash[:notice] = '款式保存成功.'
        format.html { redirect_to(styles_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @style.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /styles/1
  # DELETE /styles/1.xml
  def destroy
    @style = Style.find(params[:id])
    @style.destroy

    respond_to do |format|
      format.html { redirect_to(styles_url) }
      format.xml  { head :ok }
    end
  end

  def admin
    @styles = Style.paginate :conditions=>{:active=>0},:page => params[:page],:per_page =>24,:order=>"id desc"
  end

  def reactive
    Style.update(params[:style].keys,params[:style].values)
    redirect_to :action=>:admin
  end


end
